/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
  // var arr = [];
  // var max = 0;
  // s.split('').forEach(str => {
  //   if (arr.includes(str)) {
  //     max = Math.max(max, arr.length);
  //     arr = [str];
  //   } else {
  //     arr.push(str);
  //   }
  // })
  // return max;
  let map = new Map()
  let start = -1,
    maxLen = 0
  arr = s.split("")
  // let longStrArr = []
  arr.forEach((element, index) => {
    if (map.has(element)) {
      start = Math.max(map.get(element), start)
    }
    map.set(element, index)
    maxLen = Math.max(index - start, maxLen)
    // let strArr = arr.slice(start, maxLen);
    // longStrArr = strArr.length > longStrArr.length ? strArr : longStrArr;
  })
  return maxLen
  // return longStrArr.join('')
};

console.log(lengthOfLongestSubstring('abcabcbb'))